// -----------------------------------------------------------------------
//  <copyright file="ForeignKeyConstraintExceptionHandler.cs" company="LiuliuSoft">
//      Copyright (c) 2022-2024 DaprPlus. All rights reserved.
//  </copyright>
//  <site>https://dapr.plus</site>
//  <last-editor>郭明锋</last-editor>
//  <last-date>2024/12/8 3:02:06</last-date>
// -----------------------------------------------------------------------

using DaprPlus.AspNetCore.Mvc.Filters;

namespace DaprPlus.EntityFrameworkCore;

/// <summary>
/// 外键约束异常处理器
/// </summary>
public class ForeignKeyConstraintExceptionHandler : ApiResultExceptionHandlerBase
{
    public override int Priority => 100;

    /// <inheritdoc />
    public override bool CanHandle(Exception exception)
    {
        var message = exception.Message;
        return message.Contains("REFERENCE constraint") ||
               message.Contains("FK_") ||
               message.Contains("foreign key constraint") ||
               message.Contains("外键约束") ||
               message.Contains("违反引用完整性约束");
    }

    /// <inheritdoc />
    protected override string GetFriendlyMessage(Exception exception)
    {
        return "该数据正在被其他数据引用，无法删除。请先删除相关数据后再试。";
    }
}